package com.rong.shop.service.impl;

import com.rong.shop.mapper.AddressDistrictMapper;
import com.rong.shop.mapper.AddressMapper;
import com.rong.shop.pojo.Address;
import com.rong.shop.service.IAddressService;
import com.rong.shop.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private AddressDistrictMapper addressDistrictMapper;

    /**
     * 新增地址数据，条件：用户下的地址数不能 > 20
     *                 如果是第一条，就设置为默认收货地址
     * @param uid           从前端传来的uid
     * @param username      从前端传来的username
     * @param address       从前端传来的address数据对象
     */
    @Override
    public void addAddress(Integer uid, String username, Address address) {

        Integer rows = addressMapper.countAddressByUid(uid);
        if(rows >= 20){
            throw new AddressCountLimitException("用户添加的收货地址大于20条");
        }

        if(rows == 0){
            address.setIsDefault(1);
        }else {
            address.setIsDefault(0);
        }

        address.setUid(uid);
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date());
        address.setModifiedUser(username);

        // 从代码中获取名字
        address.setProvinceName(addressDistrictMapper.findNameByCode(address.getProvinceCode()));
        address.setCityName(addressDistrictMapper.findNameByCode(address.getCityCode()));
        address.setAreaName(addressDistrictMapper.findNameByCode(address.getAreaCode()));

        Integer result = addressMapper.insertAddress(address);

        if(result < 1){
            throw new InsertException("插入时发生未知错误");
        }

    }

    /**
     * 根据前端传来的uid，进行查询返回list<Address>对象
     * @param uid
     * @return List<Address>
     */
    @Override
    public List<Address> findByUid(Integer uid) {
        List<Address> addresses = addressMapper.findByUid(uid);
        return addresses;
    }

    /**
     * 根据前端传来的 aid ,进行查询返回Address对象
     * @param aid           前端传来的 aid
     * @return Address
     */
    @Override
    public Address findByAid(Integer aid) {
        Address address = addressMapper.findByAid(aid);
        return address;
    }

    /**
     * 根据前端传来的aid，进行设置默认值
     * @param aid           Js前端传来的
     * @param uid           session里获取
     * @param username      session里获取
     */
    @Override
    public void setDefault(Integer aid, Integer uid, String username) {

        Address result = addressMapper.findByAid(aid);
        if(result == null){
            throw new AddressNotFoundException("收货地址不存在");
        }

        if(result.getUid() != uid){
            throw new AccessDeniedException("非法数据访问!");
        }

        // 先把 用户 所有的设置为0
        Integer noRows = addressMapper.updateNoDefaultByUid(uid);
        if(noRows < 1){
            throw new UpdateException("数据更新异常");
        }

        // 把 地址 设置为1
        Integer rows = addressMapper.updateDefaultByAid(aid, username, new Date());
        if(rows < 1){
            throw new UpdateException("数据更新异常");
        }

    }


     /**
     * 根据前端传来的 aid 进行删除操作
     * @param aid
     * @param uid
     */
    @Override
    public void deleteByAid(Integer aid, Integer uid) {

        Address result = addressMapper.findByAid(aid);
        if(result == null){
            throw new AddressNotFoundException("收货地址不存在");
        }

        if(!uid.equals(result.getUid())){
            throw new AccessDeniedException("数据非法访问！");
        }

        Integer rows = addressMapper.deleteByAid(aid);
        if(rows < 1){
            throw new DeleteException("删除异常");
        }

    }
}
